﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4App.Areas.Administrators.Models;
using MVC4App.Repositories.Core;
using MVC4App.Repositories.Core.Security;
using MVC4App.Repositories.Implementations.Security;
using MVC4App.SecurityServiceReference;
using Newtonsoft.Json;

namespace MVC4App.Areas.Administrators.Controllers
{
    public class SiteController : AdminBaseController, IAdminBaseController
    {
        private readonly ISiteRepository _siteRepository;
        private readonly IPermissionRepository _permissionRepository;
        //
        // GET: /Administrators/Site/
        public SiteController()
        {
            _siteRepository = new SiteRepository();
            _permissionRepository=new PermissionRepository();
        }        
        [HttpGet]
        public PartialViewResult Insert(string action)
        {            
            ViewBag.Parents = _siteRepository.GetListByParentId(0).ToModel();
            return PartialView("Detail");
        }

        [HttpPost]
        public JsonResult Insert(SiteModel model)
        {    
            if(ModelState.IsValid)
            {
                 
            }
            return Json(new{message=""});
        }
        [HttpGet]
        public PartialViewResult GetPaged(int currentPage = 1)
        {                   
            var totalPage = 0;
            var result = _siteRepository.GetPaged(ref totalPage, currentPage);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            return PartialView(result.ToModel());
        }
        /// <summary>
        /// Gets the advanced.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="status">The status.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult GetAdvanced(string keyword = null, bool? status = null, string sort = "default", int totalPage = 0, int currentPage = 1)
        {            
            if(sort.Equals("default"))
                sort = null;
            if (keyword != null && keyword.Equals(""))
                keyword = null;
            var result=_siteRepository.GetAdvanced(keyword, status,sort ,ref totalPage,currentPage);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            return PartialView(result.ToModel());
        }
        //public JsonResult GetSites()
        //{
        //    var comboboxItems = new List<ComboboxJsonModel>();
        //    comboboxItems.Add(new ComboboxJsonModel() { id = "0", text = "Danh mục cha" });
        //    var sites = _siteRepository.GetList();
        //    var parents = sites.Where(x => x.ParentId == 0);
        //    foreach (var item in parents)
        //    {
        //        var comboItem = new ComboboxJsonModel() { id = item.Id.ToString(CultureInfo.InvariantCulture), text = item.Title };
        //        var hasChildren = sites.Where(x => x.ParentId == item.Id).ToList();
        //        if (hasChildren.Count > 0)
        //        {
        //            comboItem = GetChildrenSite(sites, comboItem, item.Id);
        //        }
        //        comboboxItems.Add(comboItem);
        //    }
        //    return Json(comboboxItems);
        //}
        //protected ComboboxJsonModel GetChildrenSite(List<SiteDto> sites, ComboboxJsonModel item, int parentId)
        //{
        //    var childrens = sites.Where(x => x.ParentId == parentId);
        //    var comboboxItems = new List<ComboboxJsonModel>();
        //    foreach (var children in childrens)
        //    {
        //        comboboxItems.Add(new ComboboxJsonModel() { id = children.Id.ToString(CultureInfo.InvariantCulture), text = children.Title });
        //    }
        //    item.children = comboboxItems;
        //    return item;
        //}
    }
}
